﻿using ShawnNav;
using System.Collections.Generic;
using UnityEngine;

public class NavView : MonoBehaviour
{
    public Transform AreaIDRoot;
    public NavVector3[] verticeArr;

    public void ShowAreaIDView(NavVector3 pos, int areaID)
    {
        GameObject go = new ();
        go.name = $"areaID {areaID}";
        go.transform.SetParent(AreaIDRoot);
        go.transform.position = new Vector3(pos.x, pos.y, pos.z);
        go.transform.localEulerAngles = new Vector3(90, 0, 0);
        go.transform.localScale = Vector3.one;

        TextMesh comp = go.AddComponent<TextMesh>();
        comp.text = areaID.ToString();
        comp.anchor = TextAnchor.MiddleCenter;
        comp.alignment = TextAlignment.Center;
        comp.color = Color.grey;
        comp.fontSize = 45;
        comp.fontStyle = FontStyle.Normal;
    }

    public void ShowPathView(List<NavPoly> path)
    {
        string pathID = "PathAreaID:";
        for (int i = 0; i < path.Count; i++)
        {
            pathID = $"{pathID} {path[i].areaID}"; 
            NavPoly area = path[i] ;
            int[] indexArr = area.indexArr ;

            NavVector3 nv1, nv2;
            int count = indexArr.Length ;
            for (int j = 0, k = count - 1; j < count; k = j++)
            {
                nv1 = verticeArr[indexArr[k]];
                nv2 = verticeArr[indexArr[j]];
                ShowDebugLine(nv1, nv2, Color.yellow, 5);
            }
        }
    }

    public void ShowConnerView(List<NavVector3> connerLst)
    {
        if (connerLst?.Count > 0)
        {
            NavVector3 v1, v2;
            v1 = connerLst[0];
            for (int i = 0; i < connerLst.Count; i++)
            {
                v2 = connerLst[i];
                ShowDebugLine(v1, v2, Color.red, 10);
                v1 = v2;
            }

            string info = "";
            for (int k = 0; k < connerLst.Count; k++)
            {
                info += $"{connerLst[k]}";
            }
            this.LogYellow($"connerLst:{info}");
        }
    }

    public static void ShowDebugLine(NavVector3 nv1, NavVector3 nv2, Color color, float showTime = float.MaxValue)
    {
        Vector3 v1 = new Vector3(nv1.x, nv1.y, nv1.z);
        Vector3 v2 = new Vector3(nv2.x, nv2.y, nv2.z);
        Debug.DrawLine(v1, v2, color, showTime);
    }
}
